﻿var info = getUrlId().split('=');
var d = info[0].split('-');
// console.log(info[1])

var data;

$('#time').html(d[0] + '年' + d[1] + '月' + d[2] + '日');

// document.getElementById('nav').innerHTML = template('navs', navinfo4);
$('.indexl43').addClass('active');

var allData = {
  data: [],
  data2: []
};
loading.show();


function getProjectInfo() {
  $.ajax({
    url: '/api/CommonApi?API=GetProject',
    type: 'get',
    data: {
      ProjectID: info[2]
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        // console.log(data)
        var obj = data.ApiParamObj[0];
        var date = new Date(obj.StartDate).pattern('yyyy年MM月dd日');
        var t = obj.ProjectBrand + '-' + obj.ProjectType + '-' + date;
        $('#projectInfo h1').html(t);
        $('#projectInfo h3').html(data);
      }
    }
  });
}

$.ajax({
  url: '/api/CommonApi?API=GetTeacherApply',
  type: 'get',
  data: {
    TeacherApplyID: info[1]
  },
  dataType: 'json',
  success: function (data) {
    if (data.Success) {
      allData.data = data.ApiParamObj;
      rend(allData);
    }
  }
});

$.ajax({
  url: '/api/CommonApi?API=GetTeacherApplyLog',
  type: 'get',
  data: {
    TeacherApplyID: info[1]
  },
  dataType: 'json',
  success: function (data) {
    if (data.Success) {
      // console.log(data);
      var datas= data.ApiParamObj;
      var key = {};
      var newData = [];
      var i = 0;
      var l = datas.length;
      for (;i < l;i++) {//console.log(datas[i].TriggerMan.indexOf('?') === -1 && key[datas[i].TriggerMan])
        if (datas[i].TriggerMan.indexOf('?') === -1 && !key[datas[i].TriggerMan] ) {
          key[datas[i].TriggerMan] = true;
          newData.push(datas[i]);
          // console.log(datas[i])
        }
      }
      // console.log(newData)
      allData.data2 = newData;
      rend(allData);
    }
  }
});

function rend(o) {
  if (!o.data.length || !o.data2.length) {
    return false;
  }
  var i = 0;
  var obj = o.data2;
  var l = obj.length;
  o.teacher = false;
  console.log(o)
  for (; i < l;i++) {
    if (obj[i].TriggerMan === '老师' && obj[i].IncidentContent === '提交申请') {
      o.teacher = true;
    }
  }
  checkTeacher(o);
  document.getElementById('viewDetail').innerHTML = template('detail', o);
  getProjectInfo();
  loading.hide();
  load(o);
  // $('#detailList').modal('show');
}


/*$.ajax({
  url: '/api/CommonApi?API=GetTeacherApply',
  type: 'get',
  data: {
    TeacherApplyID: info[1]
  },
  dataType: 'json',
  success: function (data) {
    if (data.Success) {
      console.log('load success');
      // console.log(data)
      // console.log(JSON.stringify(data));
      // var div = $('form').children('div');
      var obj = {};
      obj.data = data.ApiParamObj;
      document.getElementById('viewDetail').innerHTML = template('detail', obj);
      load(obj.data);
    }
  }
});*/






function load(o) {


  $('#viewDetail').on('click', '#cancel', function () {
    // if (o.teacher && !o.run) {
    //   $('#alert h1').html('取消申请成功！');
    // } else if (o.teacher && o.run) {
    //   $('#alert h1').html('重新申请成功！');
    // }
    $.ajax({
      url: '/api/CommonApi?API=TeacherApplyTableOp',
      type: 'post',
      data: {
        ApiParamObj: JSON.stringify({
          ID: info[1],
          IsDelete: 1
        })
      },
      dataType: 'json',
      success: function (data) {
        if (data.Success) {
          console.log('submit success');
          $('#alert').modal('show');
        }
      }
    });
  });

  $('#viewDetail').on('click', '#submit', function () {
    // if (o.teacher && !o.run) {
    //   $('#alert h1').html('取消申请成功！');
    // } else if (o.teacher && o.run) {
    //   $('#alert h1').html('重新申请成功！');
    // }
    loading.show();
    $.ajax({
      url: '/api/CommonApi?API=TeacherApplyTableOp',
      type: 'post',
      data: {
        ApiParamObj: JSON.stringify({
          ID: info[1],
          IsDelete: 1
        })
      },
      dataType: 'json',
      success: function (data) {
        if (data.Success) {
          console.log('submit success');
          loading.hide();
          $('#alert').modal('show');
        }
      }
    });
  });


  $('#alert').on('click', '#return', function () {
    window.location.href = '/Teacher/Project/indexl4';
  });
}